package service

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
	"lot_admin_api/api"
	"lot_admin_api/internal/consts"
	"lot_admin_api/internal/dao"
	"lot_admin_api/internal/model"
	GenInt64Id "lot_admin_api/utility/genInt64IdOfString"
	"lot_admin_api/utility/paging"
	"lot_admin_api/utility/response"
)

type sRechargeRule struct{}

var (
	insRechargeRule = sRechargeRule{}
)

func RechargeRule() *sRechargeRule {
	return &insRechargeRule
}

// AddRechargeRule  添加充值规则
func (s *sRechargeRule) AddRechargeRule(ctx context.Context, req *api.AddRechargeRuleReq) (id string, err error) {
	var saveInfo *model.RechargeRuleIdAndInfoRes
	_ = gconv.Scan(req, &saveInfo)
	saveInfo.Id = GenInt64Id.GenInt64IdOfString() // 生成雪花id
	r := g.RequestFromCtx(ctx)
	if _, err := dao.LotRechargeRule.Ctx(ctx).Save(&saveInfo); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), "保存充值规则失败")
	}
	return saveInfo.Id, err
}

// UpdateRechargeRule 更新充值规则
func (s *sRechargeRule) UpdateRechargeRule(ctx context.Context, req *api.UpdateRechargeRuleReq) (id string, err error) {
	_, err = s.SearchRechargeRule(ctx, req.Id)
	r := g.RequestFromCtx(ctx)
	if _, err := dao.LotRechargeRule.Ctx(ctx).OmitEmpty().Where("id", req.Id).Update(req); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), "更新充值规则失败")
	}
	return req.Id, err
}

// DeleteRechargeRule  删除充值规则
func (s *sRechargeRule) DeleteRechargeRule(ctx context.Context, req *api.DeleteRechargeRuleReq) (id string, err error) {
	_, err = s.SearchRechargeRule(ctx, req.Id)
	r := g.RequestFromCtx(ctx)
	if _, err = dao.LotRechargeRule.Ctx(ctx).Where("id", req.Id).Delete(); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), "删除失败")
	}
	return req.Id, err
}

// SearchRechargeRule 判断有没有充值规则
func (s *sRechargeRule) SearchRechargeRule(ctx context.Context, Id string) (res *model.RechargeRuleDbRes, err error) {
	r := g.RequestFromCtx(ctx)
	err = dao.LotRechargeRule.Ctx(ctx).Where("id", Id).Scan(&res)
	if res == nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), "充值规则不存在")
	}
	return res, err
}

// RechargeRuleList 获取充值规则列表
func (s *sRechargeRule) RechargeRuleList(ctx context.Context, req *model.RechargeRuleListReq) (res *model.RechargeRuleListRes, err error) {
	rechargeRuleList := make([]model.RechargeRuleDbRes, 0)
	startNum, endNum := paging.Paging(req.Paging)
	condition := g.Map{
		"rule_name like ?":                     "%" + req.RuleName + "%", // 模糊查询充值规则名称
		"company_id = ? OR company_id IS NUll": req.CompanyId,
	}
	r := g.RequestFromCtx(ctx)
	// 获取充值规则列表
	if err = dao.LotRechargeRule.Ctx(ctx).OmitEmpty().WithAll().Where(condition).OrderAsc("company_id").OrderDesc("created_at").Limit(startNum, endNum).Scan(&rechargeRuleList); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), err.Error())
	}
	// 获取总条数
	countInfo, err := dao.LotRechargeRule.Ctx(ctx).OmitEmpty().Where(condition).Count()
	putCount := paging.PutCount(req.Paging, countInfo)
	res = &model.RechargeRuleListRes{
		List:    rechargeRuleList,
		PutPage: *putCount,
	}
	return res, err
}
